package com.itheima.test;


import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class studentManager {

    public static void main(String[] args) {

        //创建一个学生集合,用于存储学生对象
        ArrayList<Student> list=new ArrayList<>();


        Scanner sc=new Scanner(System.in);

        while(true){
            System.out.println("-------欢迎进入学生管理系统-------");
            System.out.println("1.添加信息");
            System.out.println("2.删除信息");
            System.out.println("3.修改信息");
            System.out.println("4.查询信息");
            System.out.println("5.退出系统");
            System.out.println("--------------------------------");

            System.out.println("请输入您的选择:");
            int choice=sc.nextInt();

            switch(choice){

                case 1:
                //    System.out.println("添加信息");
                    addStudent(list);
                    break;
                case 2:
                //    System.out.println("删除信息");
                    deleteStudent(list);
                    break;
                case 3:
                //    System.out.println("修改信息");
                    updateStudent(list);
                    break;
                case 4:
                 //   System.out.println("查询信息");
                    selectStudent(list);
                    break;
                case 5:
                    System.out.println("感谢您的使用!");
                    System.exit(0);
                default:
                    System.out.println("您输入有误!");
                    break;
            }

        }

    }

    //添加学生信息
    public static void  addStudent(ArrayList<Student> list){

        String id;

        Scanner sc=new Scanner(System.in);
        int index=-1;
        do{
        while(true){

            System.out.println("请输入学生学号:");
            id=sc.next();

            int sid=getStuId(list,id);

            if(sid==-1){
                break;
            }

        }


    System.out.println("请输入学生姓名:");
    String name=sc.next();
    System.out.println("请输入学生年龄:");
    int age=sc.nextInt();
    System.out.println("请输入学生生日:");
    String bir=sc.next();

    //添加到学生对象中
    Student st=new Student();
    st.setsId(id);
    st.setName(name);
    st.setAge(age);
    st.setBirth(bir);

    list.add(st);
    System.out.println("添加成功!");
    index=isGo();
}while(index==1);

    }

    //查询学生信息
    public static void  selectStudent(ArrayList<Student> list){
        if (list.size()==0){
            System.out.println("##请先添加学生信息##");
            return;
        }else{
            System.out.println("学号\t\t姓名\t年龄\t\t生日");

            for (int i = 0; i < list.size(); i++) {
                Student st=list.get(i);
                System.out.println(st.getsId()+"\t"+st.getName()+"\t"+st.getAge()+"岁\t"+st.getBirth());
            }


        }
    }

    //判断学号是否存在
    public static int  getStuId(ArrayList<Student> list,String  sid){

        //定义参数index
        int index=-1;

        //遍历集合
        for (int i = 0; i < list.size(); i++) {

            Student st=list.get(i);

            //判断
            if (st.getsId().equals(sid)){
                index=i;
            }

        }
        //若相等,返回索引值,不同返回-1;
        return index;
    }

    //删除学生信息
    public static void  deleteStudent(ArrayList<Student> list){

        int index = -1;
        Scanner sc=new Scanner(System.in);

        do{
         System.out.println("请输入你要删除学生的学号:");
         String sid=sc.next();
         int id=getStuId(list,sid);
         if (id==-1){
             System.out.println("该学号不存在,请核对后输入!!");
         }else{
             list.remove(id);
             System.out.println("删除成功");
         }
        index=isGo();

     }while(index==1);

    }

    //更改学生信息
    public static void  updateStudent(ArrayList<Student> list){

        int count=-1;
        Scanner sc=new Scanner(System.in);

        do {
            System.out.println("请输入要修改学生的学号:");
            String index=sc.next();

            int id=getStuId(list,index);

            if (id==-1){
                System.out.println("请检查核对学生学号!");
            }else{
                System.out.println("请输入学生学号:");
                String nId=sc.next();
                System.out.println("请输入学生姓名:");
                String name=sc.next();
                System.out.println("请输入学生年龄:");
                int sage=sc.nextInt();
                System.out.println("请输入学生生日:");
                String bir=sc.next();

                Student st=new Student();
                st.setsId(nId);
                st.setName(name);
                st.setAge(sage);
                st.setBirth(bir);

                list.set(id,st);
                System.out.println("修改成功!");
            }

            count = isGo();

        }while(count==1);

    }

    //判断是否要继续
    public static int  isGo(){
        Scanner sc=new Scanner(System.in);
        System.out.println("是否继续该操作(0:否  1:是)?");
        int index=sc.nextInt();
        if (index==1){
            return 1;
        }else{
            return 0;
        }

    }

}
